{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "class NaryNode:\n",
    "    def __init__(self, value) -> None:\n",
    "        self.value = value\n",
    "        self.children = []\n",
    "\n",
    "    def add_child(self, new_node):\n",
    "        self.children.append(new_node)\n",
    "\n",
    "    # my __str__\n",
    "    # def __str__(self) -> str:\n",
    "    #     fmt = \"{}:\".format(self.value)\n",
    "    #     for child in self.children:\n",
    "    #         fmt += \" {}\".format(child.value)\n",
    "    #     return fmt\n",
    "\n",
    "    # the solution's __str__\n",
    "    # I think it better than mine\n",
    "    def __str__(self):\n",
    "        result = f\"{self.value}:\"\n",
    "        for child in self.children:\n",
    "            result += f\" {child.value}\"\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build a test tree.\n",
    "#      Root\n",
    "#        |\n",
    "#     +--+--+\n",
    "#     A  B  C\n",
    "#     |     |\n",
    "#    +-+    +\n",
    "#    D E    F\n",
    "#    |      |\n",
    "#    +     +-+\n",
    "#    G     H I\n",
    "\n",
    "root_node = NaryNode(\"Root\")\n",
    "a_node = NaryNode(\"A\")\n",
    "b_node = NaryNode(\"B\")\n",
    "c_node = NaryNode(\"C\")\n",
    "d_node = NaryNode(\"D\")\n",
    "e_node = NaryNode(\"E\")\n",
    "f_node = NaryNode(\"F\")\n",
    "g_node = NaryNode(\"G\")\n",
    "h_node = NaryNode(\"H\")\n",
    "i_node = NaryNode(\"I\")\n",
    "\n",
    "root_node.add_child(a_node)\n",
    "root_node.add_child(b_node)\n",
    "root_node.add_child(c_node)\n",
    "a_node.add_child(d_node)\n",
    "a_node.add_child(e_node)\n",
    "c_node.add_child(f_node)\n",
    "d_node.add_child(g_node)\n",
    "f_node.add_child(h_node)\n",
    "f_node.add_child(i_node)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root: A B C\n",
      "A: D E\n",
      "B:\n",
      "C: F\n",
      "D: G\n",
      "E:\n",
      "F: H I\n",
      "G:\n",
      "H:\n",
      "I:\n"
     ]
    }
   ],
   "source": [
    "print(root_node)\n",
    "print(a_node)\n",
    "print(b_node)\n",
    "print(c_node)\n",
    "print(d_node)\n",
    "print(e_node)\n",
    "print(f_node)\n",
    "print(g_node)\n",
    "print(h_node)\n",
    "print(i_node)\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e63faaf9bc27d837377622cad56f3d439b9a97e8fa476559128293c868ce68e0"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('word_database': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
